 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace DataGui
{

    public partial class Running : Window
    {
        MainWindow main;
        Thread oThread;
        Process exeProcess;

        public Running(MainWindow m)
        {
            InitializeComponent();
            main = m;
            runDataBase();
        }

        private void cancelRuning_Click(object sender, RoutedEventArgs e)
        {
            oThread.Abort();
            foreach (System.Diagnostics.Process myProc in System.Diagnostics.Process.GetProcesses())
                if (myProc.ProcessName == "DataBase")
                    myProc.Kill();
            App.Current.MainWindow = main;
            this.Close();
            main.Show();
        }

        public void runDataBase()
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.WorkingDirectory = "..\\..\\..\\Release";
            startInfo.Domain = "..\\..\\..\\Release";
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false;
            startInfo.FileName = "..\\..\\..\\Release\\DataBase.exe";
            startInfo.WindowStyle = ProcessWindowStyle.Minimized;
            startInfo.CreateNoWindow = true;
            try
            {
                using (exeProcess = Process.Start(startInfo))
                {
                    RunDataBaseProgram prog = new RunDataBaseProgram(exeProcess);
                    prog.OnWorkerComplete += new RunDataBaseProgram.OnWorkerMethodCompleteDelegate(OnWorkerMethodComplete);
                    oThread = new Thread(new ThreadStart(prog.updateProgressBar));
                    oThread.Start();

                    this.Show();
                    int exitCode = exeProcess.ExitCode;
                }
            }
            catch
            {
                int c = 0;
            }
        }

        private void OnWorkerMethodComplete(string line,double v)
        {
                          
              progressBar.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
              new Action(
              delegate()
              {
                  if(v!=-2)
                    progressBar.Value += v;
                  else 
                  {
                      progressBar.Value = 100;
                      LtakeTime.Content = "Running complete!";
                      progressBar.Visibility = System.Windows.Visibility.Hidden;
                      BshowResults.IsEnabled = true;
                  }
              }
              ));

             
              Loutput.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
              new Action(
              delegate()
              {
                  if(line!="")
                      Loutput.Content = Loutput.Content + "\n" + line;   
              }
              ));
              
         }

        private void BshowResults_Click(object sender, RoutedEventArgs e)
        {
            oThread.Abort();
            foreach (System.Diagnostics.Process myProc in System.Diagnostics.Process.GetProcesses())
                if (myProc.ProcessName == "DataBase")
                    myProc.Kill();
            App.Current.MainWindow = new ShowResults(main, (string)Loutput.Content);
            this.Close();
            App.Current.MainWindow.Show();
        }

    }


    public class RunDataBaseProgram
    {
        public delegate void OnWorkerMethodCompleteDelegate(string message, double v);
        public event OnWorkerMethodCompleteDelegate OnWorkerComplete;

        Process exeProcess;
        public RunDataBaseProgram(Process _exeProcess)
        {
            exeProcess = _exeProcess;
        }
        public void updateProgressBar()
        {
            StreamReader sr = exeProcess.StandardOutput;
            string line;
            bool first = true;
            int times = 0;
            double v = 0;
            while ((line = sr.ReadLine()) != null)
            {
                OnWorkerComplete(line, 0);//print the next line to "Details Lable"

                if (!MainWindow.SRshow)
                {
                    if (first)
                    {
                        if (line.StartsWith("time"))
                            times++;
                        OnWorkerComplete("", 0.2); // update progress bar
                    }
                    if (line.StartsWith("Run 2"))
                    {
                        first = false;
                        v = (100 - 0.2 * times - 5) / (2 * times);
                    }
                    if (!first && line.StartsWith("time"))
                    {
                        OnWorkerComplete("", v);// update progress bar
                    }
                    if (line.StartsWith("The End"))
                        OnWorkerComplete("", -2);// update progress bar
                }
                else
                {
                    if (line.StartsWith("Number") )
                        OnWorkerComplete("",5 );// update progress bar
                    if (line.StartsWith("The End"))
                        OnWorkerComplete("", -2);// update progress bar
                }
            }
        }

    };
}
